package com.qf.jie.dao.impl;

import com.qf.jie.dao.UserDao;
import com.qf.jie.entity.po.User;
import com.qf.jie.utils.DBUtils;

import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public int addBean(User user) {
        return DBUtils.excuteUpdate("insert into user(username,password,user_type,money,sex,phone) values(?,?,?,?,?,?) ",
                user.getUsername(), user.getPassword(), user.getUser_type(), user.getMoney(),user.getSex(),user.getPhone()
                );
    }

    @Override
    public int deleteById(int id) {
        return DBUtils.excuteUpdate("delete from user where id=?", id);
    }

    @Override
    public int updateById(User user) {
        return DBUtils.excuteUpdate("update user set username=?,password=?,user_type=?,money=?,sex=?,phone=? where id=?",
                user.getUsername(), user.getPassword(), user.getUser_type(), user.getMoney(),user.getSex(),user.getPhone(),
                user.getId()
        );
    }

    @Override
    public int shamDeleteById(int id) {
        return DBUtils.excuteUpdate("update user set is_delete=0 where id=?", id);
    }


    @Override
    public int getCount() {
        return DBUtils.excuteQuery("select count(*) from user where is_delete=1");
    }

    @Override
    public int getIdByName(String name) {
        return DBUtils.excuteQuery("select id from user where username=?", name);
    }

    @Override
    public int getCountByName(String username) {
        return DBUtils.excuteQuery("select count(*) from user where username=?", username);
    }

    @Override
    public int getIdByNameAndPassword(String name, String password) {
        return DBUtils.excuteQuery("select id from user where username=? and password=? and is_delete=1", name, password);
    }

    @Override
    public List<User> getList() {
        return DBUtils.excuteQuery(User.class, "select * from user where is_delete=1");
    }

    @Override
    public List<User> getList(int start, int len) {
        return DBUtils.excuteQuery(User.class, "select * from user where is_delete=1 limit ?,?", start, len);
    }

    @Override
    public List<User> getListLike(String name) {
        return null;
    }

    @Override
    public List<User> getBeanById(int id) {
        return DBUtils.excuteQuery(User.class, "select * from user where id=? and is_delete=1",id);
    }
}
